package com.leostadyn.stackfarm.timestatistics.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.leostadyn.stackfarm.timestatistics.R;
import com.leostadyn.stackfarm.timestatistics.model.bean.ProjectBean;
import com.leostadyn.stackfarm.timestatistics.utils.TimeUtils;
import com.leostadyn.stackfarm.timestatistics.view.activity.admin.project.ProjectEditActivity;

import java.util.List;

public class ProjectListAdapter extends RecyclerView.Adapter {
    private Context mContext;
    private List<ProjectBean> projectListBeans;
    private OnDeleteButtonClick onDeleteButtonClick;

    public List<ProjectBean> getProjectListBeans() {
        return projectListBeans;
    }

    public void setProjectListBeans(List<ProjectBean> projectListBeans) {
        this.projectListBeans.clear();
        if (projectListBeans != null && projectListBeans.size() > 0) {
            this.projectListBeans.addAll(projectListBeans);
        }
        notifyDataSetChanged();

    }

    public ProjectListAdapter(Context mContext, List<ProjectBean> projectListBeans, OnDeleteButtonClick onDeleteButtonClick) {
        this.mContext = mContext;
        if (projectListBeans != null) {
            this.projectListBeans = projectListBeans;
        }
        this.onDeleteButtonClick = onDeleteButtonClick;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_project_list, viewGroup, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {

        if (viewHolder instanceof ViewHolder) {
            ProjectBean project = projectListBeans.get(i);
            ((ViewHolder) viewHolder).tv_project_name.setText(project.getName());
            ((ViewHolder) viewHolder).tv_project_state.setText("状态: " + (project.getStatus().equals("0") ? "关" : "开"));
            ((ViewHolder) viewHolder).tv_project_id.setText(project.getCode());
            ((ViewHolder) viewHolder).tv_project_time.setText(TimeUtils.getTime1(project.getCreatedTime()));

            ((ViewHolder) viewHolder).tv_progress_delete.setOnClickListener(v -> {
                onDeleteButtonClick.onBtnClicked(i, project, ((ViewHolder) viewHolder).tv_project_name,
                        ((ViewHolder) viewHolder).tv_project_state, ((ViewHolder) viewHolder).tv_project_id,
                        ((ViewHolder) viewHolder).tv_project_time);
            });

            ((ViewHolder) viewHolder).tv_progress_edit.setOnClickListener(v -> {
                Intent intent = new Intent(mContext, ProjectEditActivity.class);
                intent.putExtra("uid", project.getId());
                intent.putExtra("name", project.getName());
                intent.putExtra("code", project.getCode());
                intent.putExtra("status", project.getStatus());
                mContext.startActivity(intent);
            });
        }

    }

    @Override
    public int getItemCount() {
        return projectListBeans.size();
    }

    public interface OnDeleteButtonClick {
        void onBtnClicked(int position, ProjectBean data, TextView name, TextView state, TextView id, TextView time);
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_project_name;
        private TextView tv_project_id;
        private TextView tv_project_state;
        private TextView tv_project_time;
        private TextView tv_progress_edit;
        private TextView tv_progress_delete;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_project_name = (itemView).findViewById(R.id.tv_project_name);
            tv_project_state = (itemView).findViewById(R.id.tv_project_state);
            tv_project_id = (itemView).findViewById(R.id.tv_project_id);
            tv_project_time = (itemView).findViewById(R.id.tv_project_time);
            tv_progress_edit = (itemView).findViewById(R.id.tv_progress_edit);
            tv_progress_delete = (itemView).findViewById(R.id.tv_progress_delete);
        }
    }


}

